﻿Imports System.ComponentModel
Imports System.Web.UI
Imports System.Web.UI.WebControls

Namespace Controls
    ''' <summary> 
    ''' CheckBoxList 控件。 
    ''' </summary> 
    <DefaultProperty("Text"), ToolboxData("<{0}:CheckBoxList runat=server></{0}:CheckBoxList>")> _
    Public Class CheckBoxList
        Inherits System.Web.UI.WebControls.CheckBoxList
        Implements IWebControl
        Implements IPostBackDataHandler
        ''' <summary> 
        ''' 构造函数 
        ''' </summary> 
        Public Sub New()
            MyBase.New()
            'this.BorderStyle=BorderStyle.Dotted; 
            'this.BorderWidth=1; 
            'this.Font.Size=10; 
            Me.RepeatColumns = 2
            Me.Width = Unit.Pixel(100)
            'this.Font.Size=FontUnit.Smaller; 
            Me.RepeatDirection = WebControls.RepeatDirection.Vertical
            Me.RepeatLayout = WebControls.RepeatLayout.Table

            Me.CssClass = "buttonlist"
        End Sub

        ''' <summary> 
        ''' 添加表数据 
        ''' </summary> 
        ''' <param name="dt">要绑定的表</param> 
        Public Sub AddTableData(ByVal dt As DataTable)
            Me.Items.Clear()
            For Each r As DataRow In dt.Rows
                Me.Items.Add(New ListItem(r(1).ToString(), r(0).ToString()))
            Next
            Me.DataBind()
        End Sub

        ''' <summary> 
        ''' 添加表数据 
        ''' </summary> 
        ''' <param name="dt">要绑定的表</param> 
        ''' <param name="selectid">选取项</param> 
        Public Sub AddTableData(ByVal dt As DataTable, ByVal selectid As String)
            selectid = "," + selectid + ","

            Me.Items.Clear()
            For i As Integer = 0 To dt.Rows.Count - 1
                Me.Items.Add(New ListItem(dt.Rows(i)(1).ToString(), dt.Rows(i)(0).ToString()))

                If selectid.IndexOf("," + dt.Rows(i)(0).ToString() + ",") >= 0 Then
                    Me.Items(i).Selected = True

                End If
            Next
            Me.DataBind()
        End Sub

        ''' <summary> 
        ''' 通过ID绑定选项 
        ''' </summary> 
        ''' <param name="selectid">批定ID</param> 
        Public Sub SetSelectByID(ByVal selectid As String)
            selectid = "," + selectid + ","
            For i As Integer = 0 To Me.Items.Count - 1
                If selectid.IndexOf("," + Me.Items(i).Value + ",") >= 0 Then
                    Me.Items(i).Selected = True
                End If
            Next
            Me.DataBind()
        End Sub

        ''' <summary> 
        ''' 得到所选项的字符串(格式: 1,2,3) 
        ''' </summary> 
        ''' <returns></returns> 
        Public Function GetSelectString() As String
            Return GetSelectString(",")
        End Function

        ''' <summary> 
        ''' 得到按指定分割符的选项字符串 
        ''' </summary> 
        ''' <param name="split">指定分割符</param> 
        ''' <returns></returns> 
        Public Function GetSelectString(ByVal split As String) As String
            split = split.Trim()
            Dim result As String = Nothing
            For Each li As ListItem In Me.Items
                If li.Selected Then
                    result = result + li.Value + split
                End If
            Next
            If result Is Nothing Then
                Return ""
            End If

            Return IIf((result.LastIndexOf(split) = (result.Length - 1)), result.Substring(0, result.Length - 1), result)
        End Function

        ''' <summary> 
        ''' 得到按指定分割符的选项字符串 
        ''' </summary> 
        ''' <param name="split">指定分割符</param> 
        ''' <param name="items">列表选项(含未选中的选项)</param> 
        ''' <returns></returns> 
        Public Function GetSelectString(ByVal split As String, ByVal items As System.Web.UI.WebControls.ListItemCollection) As String
            split = split.Trim()
            Dim result As String = Nothing
            For Each li As ListItem In items
                If li.Selected Then
                    result = result + li.Value + split
                End If
            Next
            If result Is Nothing Then
                Return ""
            End If

            Return IIf((result.LastIndexOf(split) = (result.Length - 1)), result.Substring(0, result.Length - 1), result)
        End Function

#Region "SQL字符串"

        ''' <summary> 
        ''' SQL字符串变量 
        ''' </summary> 
        Private m_sqltext As String


        ''' <summary> 
        ''' SQL字符串属性 
        ''' </summary> 
        <Bindable(True), Category("Appearance"), DefaultValue("")> _
        Public Property SqlText() As String
            Get
                Return m_sqltext
            End Get

            Set(ByVal value As String)
                m_sqltext = value
            End Set
        End Property

#End Region

        ''' <summary> 
        ''' 当某选项被选中后,获取焦点的控件ID(如提交按钮等) 
        ''' </summary> 
        <Bindable(True), Category("Appearance"), DefaultValue("")> _
        Public Property SetFocusButtonID() As String
            Get
                Dim o As Object = ViewState(Me.ClientID + "_SetFocusButtonID")
                Return IIf((o Is Nothing), "", o.ToString())
            End Get
            Set(ByVal value As String)
                ViewState(Me.ClientID + "_SetFocusButtonID") = value
                If value <> "" Then
                    Me.Attributes.Add("onChange", "document.getElementById('" + value + "').focus();")
                End If
            End Set
        End Property


        Private _hintTitle As String = ""

        ''' <summary> 
        ''' 提示框标题 
        ''' </summary> 
        <Bindable(True), Category("Appearance"), DefaultValue("")> _
        Public Property HintTitle() As String Implements IWebControl.HintTitle
            Get
                Return _hintTitle
            End Get
            Set(ByVal value As String)
                _hintTitle = value
            End Set
        End Property


        Private _hintInfo As String = ""

        ''' <summary> 
        ''' 提示框内容 
        ''' </summary> 
        <Bindable(True), Category("Appearance"), DefaultValue("")> _
        Public Property HintInfo() As String Implements IWebControl.HintInfo
            Get
                Return _hintInfo
            End Get
            Set(ByVal value As String)
                _hintInfo = value
            End Set
        End Property

        Private _hintLeftOffSet As Integer = 0

        ''' <summary> 
        ''' 提示框左侧偏移量 
        ''' </summary> 
        <Bindable(True), Category("Appearance"), DefaultValue(0)> _
        Public Property HintLeftOffSet() As Integer Implements IWebControl.HintLeftOffSet
            Get
                Return _hintLeftOffSet
            End Get
            Set(ByVal value As Integer)
                _hintLeftOffSet = value
            End Set
        End Property

        Private _hintTopOffSet As Integer = 0

        ''' <summary> 
        ''' 提示框顶部偏移量 
        ''' </summary> 
        <Bindable(True), Category("Appearance"), DefaultValue(0)> _
        Public Property HintTopOffSet() As Integer Implements IWebControl.HintTopOffSet
            Get
                Return _hintTopOffSet
            End Get
            Set(ByVal value As Integer)
                _hintTopOffSet = value
            End Set
        End Property

        Private _hintShowType As String = "up"
        '或"down" 
        ''' <summary> 
        ''' 提示框风格,up(上方显示)或down(下方显示) 
        ''' </summary> 
        <Bindable(True), Category("Appearance"), DefaultValue("up")> _
        Public Property HintShowType() As String
            Get
                Return _hintShowType
            End Get
            Set(ByVal value As String)
                _hintShowType = value
            End Set
        End Property

        Private _hintHeight As Integer = 50

        ''' <summary> 
        ''' 提示框高度 
        ''' </summary> 
        <Bindable(True), Category("Appearance"), DefaultValue(130)> _
        Public Property HintHeight() As Integer Implements IWebControl.HintHeight
            Get
                Return _hintHeight
            End Get
            Set(ByVal value As Integer)
                _hintHeight = value
            End Set
        End Property

        ''' <summary> 
        ''' 输出html,在浏览器中显示控件 
        ''' </summary> 
        ''' <param name="output"> 要写出到的 HTML 编写器 </param> 
        Protected Overloads Overrides Sub Render(ByVal output As HtmlTextWriter)
            If Me.HintInfo <> "" Then
                output.WriteBeginTag("span id=""" + Me.ClientID + """ onmouseover=""showhintinfo(this," + Me.HintLeftOffSet + "," + Me.HintTopOffSet + ",'" + Me.HintTitle + "','" + Me.HintInfo + "','" + Me.HintHeight + "','" + Me.HintShowType + "');"" onmouseout=""hidehintinfo();"">")
            End If

            MyBase.Render(output)

            If Me.HintInfo <> "" Then
                output.WriteEndTag("span")
            End If
        End Sub

    End Class
End Namespace